﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.Unity;
using Ymca.Web.Services.Interfaces;
using Ymca.DataAccess.Interfaces;
using Ymca.Web.Models.Blog;
using Ymca.Entities;
using Ymca.DataAccess;
using PagedList;
using Ymca.Entities.Enums;

namespace Ymca.Web.Services
{
    public class PostsService: IPostsService
    {
        private IUnityContainer _container;
        private IPostBuilder _postBuilder;
        private IAttachmentBuilder _attachmentBuilder;
        private ICommentBuilder _commentBuilder;
		private IGalleryService _galleryService;
		private IDataContext _dataContext;

        public PostsService()
        {
            _container = ContainerConfig.Container;
            _postBuilder = _container.Resolve<IPostBuilder>();
            _attachmentBuilder = _container.Resolve<IAttachmentBuilder>();
            _commentBuilder = _container.Resolve<ICommentBuilder>();
			_galleryService = _container.Resolve<IGalleryService>();
			_dataContext = new DataContext();
        }

        public void AddPost(CreatePostViewModel viewModel)
        {
			//TODO: Devide in logical parts to different classes and builders.
			Post post = new Post();
			post.Date = DateTime.Now;
			post.Title = viewModel.Title;
			post.Body = viewModel.Body;
			post.Comments = new List<Comment>();
			if (viewModel.Attachments.Count() > 1)
			{
				GalleryGroup gallery = _galleryService.CreateGallery(viewModel.Title);
				gallery.ShowInGallery = false;
				gallery.GalleryItems = new List<GalleryItem>();
				foreach (HttpPostedFileBase file in viewModel.Attachments)
				{
					if (file != null)
					{
						GalleryItem attachment = _attachmentBuilder.Create(file);
						gallery.GalleryItems.Add(attachment);
					}
				}
				post.Gallery = gallery;
			}

			_dataContext.Posts.Add(post);
			_dataContext.Save();
        }

        public bool DeletePost(int id)
        {
			Post post = _dataContext.Posts.Find(id);
			if (post != null)
			{
				//post.Attachments = _dataContext.GalleryItems.Where(a => a.PostId.Equals(post.Id)).ToList();
				//post.Comments = _dataContext.Comments.Where(c => c.PostId.Equals(post.Id)).ToList();
				//foreach (GalleryItem attachment in post.Attachments)
				//{
				//	System.IO.File.Delete(attachment.Url);
				//}
				//_dataContext.Posts.Remove(post);
				//_dataContext.Save();
				//return true;
			}
			return false;
        }

        public Post EditPost(Post post)
        {
            throw new NotImplementedException();
        }

		public Post GetPost(int id)
		{
			return _dataContext.Posts.Find(id);
		}

        public IEnumerable<Post> GetPosts()
        {
			IEnumerable<Post> posts = _dataContext.Posts;
			return posts;
        }

		public IEnumerable<Post> GetPostsByDate(DateTime date)
		{
			throw new NotImplementedException();
		}

		public void AddComment(int postId, string userName, string comment)
		{
			Post post = GetPost(postId);
			if (post.Comments == null)
			{
				post.Comments = new List<Comment>();
			}
			Comment cmt = _commentBuilder.Create(postId, userName, comment);
			post.Comments.Add(cmt);
			_dataContext.Save();
		}

		#region Private methods
		#endregion
	}
}